var mmlog=require("../sever/mmlog").root("[routes/session.js]");

var express = require('express');
var router = express.Router();
var _session = require("../sever/session");
var _dbSession = require("../sever/db/session");
var _dbCustomer = require("../sever/db/customer");
var _util = require("../sever/util");

router.get('/summary/:keyId', function(req, res, next) {
	var id = req.params.keyId;
	var s = _session.getSessionByKeyId(id);
	res.render("summary", s);
});

/**
 *提交会话小结 
 */
router.post('/summary/submit', function(req, res, next) {
	var data = req.body;
	_session.submitSummary(data,function(result){
		res.json(result);
	});
});
router.post('/send', function(req, res, next) {
	var data = req.body;
});
router.get('/close/:keyId', function(req, res, next) {
	var data = req.params.keyId;
	_session.closeSession(data);
	res.json(_util.result(0));
});

router.post('/query/:jobId', function(req, res, next) {
	var data = req.body;
	var jobId = req.params.jobId;
	_dbSession.querySession(jobId, function(result) {
		res.json(result);
	});
});
router.get("/query", function(req, res, next) {
	res.json(_session.query());
});
router.get("/unfinish/:jobId", function(req, res, next) {
	var jobId = req.params.jobId;
	if (jobId) {
		res.json(_util.result(0,_session.loadUnfinishSession(jobId)));
	} else {
		res.json(_util.result(101002))
	}
});

router.get("/report/hour/session",function(req,res,next){
	_session.sessionReport("h",function(data){
		res.json(data)
	})
})
router.get("/report/agent/session",function(req,res,next){
	_session.sessionReport("a",function(data){
		res.json(data)
	})
})
router.get("/report/business/session",function(req,res,next){
	_session.sessionReport("b",function(data){
		res.json(data)
	})
})

router.post("/update/customer/:id/:key",function(req,res,next){
	var id=req.params.id,
		key=req.params.key;
	var data=req.body;
	_dbCustomer.updateCustomer(id,data,function(data){
		if(data!=-1){
			_session.updateCustomer(key,data);
			res.json(_util.result(0));
		}else{
			res.json(_util.result(101003));
		}
	})
})


module.exports = router;